﻿using System;
using System.Data;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class BasicSalary
    {
        public static SalaryInfo TinhLuong(SalaryInfo _SalaryInfo, int _TotalDate, int _Day, decimal _BasicSalary, decimal _TotalFamilySalary,
            decimal _SocialPercent, decimal _MedicalPercent, decimal _SocialInc, decimal _MedicalInc,
            bool _SocialEmployee, bool _MedicalEmployee, decimal _FactorLeave, bool _IsInsurance)
        {
            //Tính lương:

            //Số ngày làm việc:
            decimal _DateWork;

            if (_Day > 0)
                _DateWork = _TotalDate - _SalaryInfo.TotalDateOutWork + _Day;
            else
                _DateWork = _TotalDate - _SalaryInfo.TotalDateOutWork;
            if (_DateWork > 0)
                _SalaryInfo.TotalDateWork = _DateWork;
            else
            {
                _DateWork = 0;
                _SalaryInfo.TotalDateWork = 0;
            }
            //Thâm niên vượt khung:
            decimal _OverSeniorFactor;
            _OverSeniorFactor = _SalaryInfo.OverSeniorityFactor / 100 * _SalaryInfo.BasicFactor;

            //Thâm niên nghề:
            decimal _SeniorFactor;
            _SeniorFactor = _SalaryInfo.SeniorityFactor / 100 * (_SalaryInfo.BasicFactor + _SalaryInfo.PositionFactor + _OverSeniorFactor);

            //Lương trung bình 1 ngày làm việc:
            decimal _BasicSalaryOneDay, _PositionAllowanceOneDay;
            decimal _ResponseSalaryOneDay, _ComSalaryOneDay;
            decimal _OverSeniorityAllowanceOneDay, _SeniorityAllowanceOneDay;

            _BasicSalaryOneDay = (_SalaryInfo.BasicFactor + _SalaryInfo.PositionFactor + _OverSeniorFactor + _SeniorFactor) * _BasicSalary / _TotalDate * _SalaryInfo.ProbationFactor;

            _PositionAllowanceOneDay = _SalaryInfo.PositionFactor * _BasicSalary / _TotalDate * _SalaryInfo.ProbationFactor;

            _OverSeniorityAllowanceOneDay = _OverSeniorFactor * _BasicSalary / _TotalDate * _SalaryInfo.ProbationFactor;

            _SeniorityAllowanceOneDay = _SeniorFactor * _BasicSalary / _TotalDate * _SalaryInfo.ProbationFactor;

            _ComSalaryOneDay = _SalaryInfo.CompanyFactor * _BasicSalary / _TotalDate * _SalaryInfo.ProbationFactor;

            _ResponseSalaryOneDay = _SalaryInfo.ResponseFactor * _BasicSalary / _TotalDate * _SalaryInfo.ProbationFactor;

            //Tính các chỉ số lương:
            _SalaryInfo.BasicSalary = _BasicSalaryOneDay * (_DateWork + _SalaryInfo.TotalLeavePaid * _FactorLeave);
            _SalaryInfo.PositionAllowance = _PositionAllowanceOneDay * (_DateWork + _SalaryInfo.TotalLeavePaid * _FactorLeave);
            _SalaryInfo.OverSeniorityAllowance = _OverSeniorityAllowanceOneDay * (_DateWork + _SalaryInfo.TotalLeavePaid * _FactorLeave);
            _SalaryInfo.SeniorityAllowance = _SeniorityAllowanceOneDay * (_DateWork + _SalaryInfo.TotalLeavePaid * _FactorLeave);
            _SalaryInfo.CompanySalary = _ComSalaryOneDay * (_DateWork + _SalaryInfo.TotalLeavePaid * _FactorLeave);
            _SalaryInfo.ResponseSalary = _ResponseSalaryOneDay * (_DateWork + _SalaryInfo.TotalLeavePaid * _FactorLeave);


            //Tổng lương:
            decimal _TotalSalary;
            _TotalSalary = (_SalaryInfo.BasicSalary + _SalaryInfo.CompanySalary
                    + _SalaryInfo.ResponseSalary + _SalaryInfo.PositionAllowance
                    + _SalaryInfo.OverSeniorityAllowance + _SalaryInfo.SeniorityAllowance);

            //Tính tiền bảo hiểm:
            if (_IsInsurance)
            {
                if (_SalaryInfo.TotalDateWork < 15)
                {
                    if (_SocialEmployee && !_MedicalEmployee)
                    {
                        _SalaryInfo.EmployeeSocialInc = _BasicSalaryOneDay * _TotalDate * _SocialPercent;
                        _SalaryInfo.EmployeeMedicalInc = 0;
                        _SalaryInfo.SocialInc = _BasicSalaryOneDay * _TotalDate * _SocialInc;
                        _SalaryInfo.MedicalInc = 0;
                    }
                    else if (!_SocialEmployee && _MedicalEmployee)
                    {
                        _SalaryInfo.EmployeeSocialInc = 0;
                        _SalaryInfo.EmployeeMedicalInc = _BasicSalaryOneDay * _TotalDate * _MedicalPercent;
                        _SalaryInfo.SocialInc = 0;
                        _SalaryInfo.MedicalInc = _BasicSalaryOneDay * _TotalDate * _MedicalInc;
                    }
                    else if (_SocialEmployee && _MedicalEmployee)
                    {
                        _SalaryInfo.EmployeeSocialInc = _BasicSalaryOneDay * _TotalDate * _SocialPercent;
                        _SalaryInfo.EmployeeMedicalInc = _BasicSalaryOneDay * _TotalDate * _MedicalPercent;
                        _SalaryInfo.SocialInc = _BasicSalaryOneDay * _TotalDate * _SocialInc;
                        _SalaryInfo.MedicalInc = _BasicSalaryOneDay * _TotalDate * _MedicalInc;
                    }
                }
                else
                {
                    _SalaryInfo.EmployeeSocialInc = _BasicSalaryOneDay * _TotalDate * _SocialPercent;
                    _SalaryInfo.EmployeeMedicalInc = _BasicSalaryOneDay * _TotalDate * _MedicalPercent;
                    _SalaryInfo.SocialInc = _BasicSalaryOneDay * _TotalDate * _SocialInc;
                    _SalaryInfo.MedicalInc = _BasicSalaryOneDay * _TotalDate * _MedicalInc;
                }
            }
            else
            {
                _SalaryInfo.EmployeeSocialInc = 0;
                _SalaryInfo.EmployeeMedicalInc = 0;
                _SalaryInfo.SocialInc = 0;
                _SalaryInfo.MedicalInc = 0;
            }
            //Tính thuế thu nhập cá nhân:
            decimal _salary = 0, _incometax = 0;
            TaxClient wcfTax = Service.Tax();
            DataTable dt = new DataTable();

            if (_TotalFamilySalary > 0)
            {
                _salary = _TotalSalary - _TotalFamilySalary;
                dt = wcfTax.Tax_GetValue(_salary).Tables[0];
                if (_salary > 0)
                    _incometax = Convert.ToDecimal(dt.Rows[0][0]);
            }
            else
            {
                _salary = _TotalSalary;
                dt = wcfTax.Tax_GetValue(_salary).Tables[0];
                if (dt.Rows[0][0] == DBNull.Value)
                    _incometax = 0;
                else
                    _incometax = Convert.ToDecimal(dt.Rows[0][0]);
            }
            _SalaryInfo.IncomeTax = _incometax;

            return _SalaryInfo;
        }

        public static SalaryInfo TinhLuongMayChamCong(SalaryInfo _SalaryInfo, int _TotalDateWorkCompany,
           decimal _TotalDate, decimal _TotalOverTime, decimal _Day, decimal _NewOverTime,
            decimal _BasicSalary, decimal _TotalFamilySalary, decimal _SocialPercent, decimal _MedicalPercent,
            decimal _SocialInc, decimal _MedicalInc, bool _SocialEmployee, bool _MedicalEmployee,
            decimal _TradeUnion, decimal _UnemployedIns, decimal _UnemployedInsCompany, decimal _FactorLeave, bool _IsInsurance)
        {
            //Tính lương:

            //Số ngày làm việc:
            decimal _DateWork;
            decimal _OverTime;

            if (_Day > 0)
            {
                _DateWork = _TotalDate + _Day;
                _OverTime = _TotalOverTime + _NewOverTime;
            }
            else
            {
                _DateWork = _TotalDate;
                _OverTime = _TotalOverTime;
            }
            if (_DateWork > 0)
            {
                _SalaryInfo.TotalDateWork = _DateWork;
                _SalaryInfo.OverTime = _OverTime;
            }
            else
            {
                _DateWork = 0;
                _SalaryInfo.TotalDateWork = 0;
                _SalaryInfo.OverTime = 0;
            }

            //Lương trung bình 1 ngày làm việc:

            decimal _BasicSalaryOneDay = 0;
            decimal _PositionAllowanceOneDay = 0;
            decimal _TotalSalary = 0;

            _BasicSalaryOneDay = _SalaryInfo.BasicFactor / _TotalDateWorkCompany * _SalaryInfo.ProbationFactor;
            _PositionAllowanceOneDay = _SalaryInfo.PositionFactor / _TotalDateWorkCompany * _SalaryInfo.ProbationFactor;

            //Tính các chỉ số lương:
            _SalaryInfo.BasicSalary = _BasicSalaryOneDay * (_DateWork + _SalaryInfo.TotalLeavePaid * _FactorLeave);
            _SalaryInfo.PositionAllowance = _PositionAllowanceOneDay * (_DateWork + _SalaryInfo.TotalLeavePaid * _FactorLeave);
            _SalaryInfo.TradeUnion = _SalaryInfo.BasicFactor * _TradeUnion;
            //Tổng lương:
            _TotalSalary = (_SalaryInfo.BasicSalary + _SalaryInfo.PositionAllowance);

            //Tính tiền bảo hiểm:
            if (_IsInsurance)
            {
                if (_SocialEmployee && !_MedicalEmployee)//Đi nước ngoài:
                {
                    _SalaryInfo.EmployeeSocialInc = _BasicSalary * _SocialPercent;
                    _SalaryInfo.EmployeeMedicalInc = 0;
                    _SalaryInfo.SocialInc = _BasicSalary * _SocialInc;
                    _SalaryInfo.MedicalInc = 0;
                }
                else if (!_SocialEmployee && _MedicalEmployee)//Thai sản:
                {
                    _SalaryInfo.EmployeeSocialInc = 0;
                    _SalaryInfo.EmployeeMedicalInc = _BasicSalary * _MedicalPercent;
                    _SalaryInfo.SocialInc = 0;
                    _SalaryInfo.MedicalInc = _BasicSalary * _MedicalInc;
                }
                else//Bình thường:
                {
                    _SalaryInfo.EmployeeSocialInc = _BasicSalary * _SocialPercent;
                    _SalaryInfo.EmployeeMedicalInc = _BasicSalary * _MedicalPercent;
                    _SalaryInfo.SocialInc = _BasicSalary * _SocialInc;
                    _SalaryInfo.MedicalInc = _BasicSalary * _MedicalInc;
                    _SalaryInfo.UnemployedIns = _BasicSalary * _UnemployedIns;
                    _SalaryInfo.UnemployedInsCompany = _BasicSalary * _UnemployedInsCompany;
                }
            }
            else
            {
                _SalaryInfo.EmployeeSocialInc = 0;
                _SalaryInfo.EmployeeMedicalInc = 0;
                _SalaryInfo.SocialInc = 0;
                _SalaryInfo.MedicalInc = 0;
                _SalaryInfo.UnemployedIns = 0;
                _SalaryInfo.UnemployedInsCompany = 0;
            }
            decimal _NetPay = 0;
            _NetPay = _TotalSalary + _SalaryInfo.Award
                - (_SalaryInfo.TradeUnion + _SalaryInfo.EmployeeSocialInc + _SalaryInfo.EmployeeMedicalInc
                       + _SalaryInfo.UnemployedIns);

            //Tính thuế thu nhập cá nhân:
            decimal _salary = 0, _incometax = 0;
            TaxClient wcfTax = Service.Tax();
            DataTable dt = new DataTable();

            if (_TotalFamilySalary > 0)
            {
                _salary = _TotalSalary - _TotalFamilySalary;
                dt = wcfTax.Tax_GetValue(_salary).Tables[0];
                if (_salary > 0)
                    _incometax = Convert.ToDecimal(dt.Rows[0][0]);
            }
            else
            {
                _salary = _TotalSalary;
                dt = wcfTax.Tax_GetValue(_salary).Tables[0];
                if (dt.Rows[0][0] == DBNull.Value)
                    _incometax = 0;
                else
                    _incometax = Convert.ToDecimal(dt.Rows[0][0]);
            }
            _SalaryInfo.IncomeTax = _incometax;

            return _SalaryInfo;
        }

        public static LaterInsuranceInfo TruyDong(LaterInsuranceInfo _LaterInsuranceInfo, int _TotalDate, decimal _BasicSalary,
            decimal _SocialPercent, decimal _MedicalPercent, decimal _SocialInc, decimal _MedicalInc,
            bool _SocialEmployee, bool _MedicalEmployee, decimal _FactorLeave)
        {
            //Tính lương:

            //Số ngày làm việc:
            int _DateWork;
            _DateWork = _TotalDate - _LaterInsuranceInfo.TotalDateOutWork;
            _LaterInsuranceInfo.TotalDateWork = _DateWork;

            //Thâm niên vượt khung:
            decimal _OverSeniorFactor;
            _OverSeniorFactor = _LaterInsuranceInfo.OverSeniorityFactor / 100 * _LaterInsuranceInfo.BasicFactor;

            //Thâm niên nghề:
            decimal _SeniorFactor;
            _SeniorFactor = _LaterInsuranceInfo.SeniorityFactor / 100 * (_LaterInsuranceInfo.BasicFactor + _LaterInsuranceInfo.PositionFactor + _OverSeniorFactor);

            //Lương trung bình 1 ngày làm việc:
            decimal _SalaryOneDay;
            _SalaryOneDay = (_LaterInsuranceInfo.BasicFactor + _LaterInsuranceInfo.PositionFactor + _OverSeniorFactor + _SeniorFactor) * _BasicSalary / _TotalDate;

            _LaterInsuranceInfo.BasicSalary = _SalaryOneDay * _DateWork * _LaterInsuranceInfo.ProbationFactor + (_LaterInsuranceInfo.TotalLeavePaid * _SalaryOneDay * _FactorLeave);
            _LaterInsuranceInfo.CompanySalary = _LaterInsuranceInfo.CompanyFactor * _BasicSalary / _TotalDate * (_DateWork * _LaterInsuranceInfo.ProbationFactor + _LaterInsuranceInfo.TotalLeavePaid * _FactorLeave);
            _LaterInsuranceInfo.ResponseSalary = _LaterInsuranceInfo.ResponseFactor * _BasicSalary / _TotalDate * (_DateWork * _LaterInsuranceInfo.ProbationFactor + _LaterInsuranceInfo.TotalLeavePaid * _FactorLeave);
            _LaterInsuranceInfo.PositionAllowance = _LaterInsuranceInfo.PositionFactor * _BasicSalary / _TotalDate * (_DateWork * _LaterInsuranceInfo.ProbationFactor + _LaterInsuranceInfo.TotalLeavePaid * _FactorLeave);
            _LaterInsuranceInfo.OverSeniorityAllowance = _OverSeniorFactor * _BasicSalary / _TotalDate * (_DateWork * _LaterInsuranceInfo.ProbationFactor + _LaterInsuranceInfo.TotalLeavePaid * _FactorLeave);
            _LaterInsuranceInfo.SeniorityAllowance = _SeniorFactor * _BasicSalary / _TotalDate * (_DateWork * _LaterInsuranceInfo.ProbationFactor + _LaterInsuranceInfo.TotalLeavePaid * _FactorLeave);

            if (_LaterInsuranceInfo.TotalDateWork < 15)
            {
                if (_SocialEmployee && !_MedicalEmployee)
                {
                    _LaterInsuranceInfo.EmployeeSocialInc = _SalaryOneDay * _TotalDate * _SocialPercent;
                    _LaterInsuranceInfo.EmployeeMedicalInc = 0;
                    _LaterInsuranceInfo.SocialInc = _SalaryOneDay * _TotalDate * _SocialInc;
                    _LaterInsuranceInfo.MedicalInc = 0;
                }
                else if (!_SocialEmployee && _MedicalEmployee)
                {
                    _LaterInsuranceInfo.EmployeeSocialInc = 0;
                    _LaterInsuranceInfo.EmployeeMedicalInc = _SalaryOneDay * _TotalDate * _MedicalPercent;
                    _LaterInsuranceInfo.SocialInc = 0;
                    _LaterInsuranceInfo.MedicalInc = _SalaryOneDay * _TotalDate * _MedicalInc;
                }
                else if (_SocialEmployee && _MedicalEmployee)
                {
                    _LaterInsuranceInfo.EmployeeSocialInc = _SalaryOneDay * _TotalDate * _SocialPercent;
                    _LaterInsuranceInfo.EmployeeMedicalInc = _SalaryOneDay * _TotalDate * _MedicalPercent;
                    _LaterInsuranceInfo.SocialInc = _SalaryOneDay * _TotalDate * _SocialInc;
                    _LaterInsuranceInfo.MedicalInc = _SalaryOneDay * _TotalDate * _MedicalInc;
                }
            }
            else
            {
                _LaterInsuranceInfo.EmployeeSocialInc = _SalaryOneDay * _TotalDate * _SocialPercent;
                _LaterInsuranceInfo.EmployeeMedicalInc = _SalaryOneDay * _TotalDate * _MedicalPercent;
                _LaterInsuranceInfo.SocialInc = _SalaryOneDay * _TotalDate * _SocialInc;
                _LaterInsuranceInfo.MedicalInc = _SalaryOneDay * _TotalDate * _MedicalInc;
            }

            return _LaterInsuranceInfo;
        }
    }
}
